<?php
class laporan extends CI_Model {

	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function view(){
        $query = $this->db->query("SELECT b.kodebarang,b.nama as nama, (SELECT SUM(masuk)-sum(keluar) from laporan WHERE tanggal < LAST_DAY(DATE(NOW())) GROUP BY idbarang) as awal, masuk, keluar from laporan as l join barang as b on b.idbarang=l.idbarang where tanggal = LAST_DAY(DATE(NOW()))");
        if ($query->num_rows > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }

    function month($tanggal){
        $query = $this->db->query("SELECT b.kodebarang,b.nama as nama, (SELECT SUM(masuk)-sum(keluar) from laporan WHERE tanggal < LAST_DAY('".$tanggal."') GROUP BY idbarang) as awal, masuk, keluar from laporan as l join barang as b on b.idbarang=l.idbarang where tanggal = LAST_DAY('".$tanggal."')");
        if ($query->num_rows > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }

    function viewr(){
        $query = $this->db->query("SELECT b.kodebarang,b.nama as nama, (SELECT SUM(rmasuk)-sum(rkeluar) from laporan WHERE tanggal < LAST_DAY(DATE(NOW())) GROUP BY idbarang) as awal, rmasuk as masuk, rkeluar as keluar from laporan as l join barang as b on b.idbarang=l.idbarang where tanggal = LAST_DAY(DATE(NOW()))");
        if ($query->num_rows > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }

    function monthr($tanggal){
        $query = $this->db->query("SELECT b.kodebarang,b.nama as nama, (SELECT SUM(rmasuk)-sum(rkeluar) from laporan WHERE tanggal < LAST_DAY('".$tanggal."') GROUP BY idbarang) as awal, rmasuk as masuk, rkeluar as keluar from laporan as l join barang as b on b.idbarang=l.idbarang where tanggal = LAST_DAY('".$tanggal."')");
        if ($query->num_rows > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }

}